<template>
  <el-container class="layout-container-demo">
    <el-aside width="220px" class="sidebar">
      <el-scrollbar>
        <el-menu :default-openeds="['1', '3']" class="custom-menu">
          <el-sub-menu index="1">
            <template #title>
              <el-icon><message /></el-icon>导航一
            </template>
            <el-menu-item-group>
              <template #title>组 1</template>
              <el-menu-item index="1-1">选项 1</el-menu-item>
              <el-menu-item index="1-2">选项 2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="组 2">
              <el-menu-item index="1-3">选项 3</el-menu-item>
            </el-menu-item-group>
            <el-sub-menu index="1-4">
              <template #title>选项 4</template>
              <el-menu-item index="1-4-1">选项 4-1</el-menu-item>
            </el-sub-menu>
          </el-sub-menu>

          <el-sub-menu index="2">
            <template #title>
              <el-icon><icon-menu /></el-icon>导航二
            </template>
            <el-menu-item-group>
              <template #title>组 1</template>
              <el-menu-item index="2-1">选项 1</el-menu-item>
              <el-menu-item index="2-2">选项 2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="组 2">
              <el-menu-item index="2-3">选项 3</el-menu-item>
            </el-menu-item-group>
            <el-sub-menu index="2-4">
              <template #title>选项 4</template>
              <el-menu-item index="2-4-1">选项 4-1</el-menu-item>
            </el-sub-menu>
          </el-sub-menu>

          <el-sub-menu index="3">
            <template #title>
              <el-icon><setting /></el-icon>导航三
            </template>
            <el-menu-item-group>
              <template #title>组 1</template>
              <el-menu-item index="3-1">选项 1</el-menu-item>
              <el-menu-item index="3-2">选项 2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="组 2">
              <el-menu-item index="3-3">选项 3</el-menu-item>
            </el-menu-item-group>
            <el-sub-menu index="3-4">
              <template #title>选项 4</template>
              <el-menu-item index="3-4-1">选项 4-1</el-menu-item>
            </el-sub-menu>
          </el-sub-menu>
        </el-menu>
      </el-scrollbar>
    </el-aside>

    <el-container>
      <el-header class="header">
        <div class="toolbar">
          <img 
            src="https://daiyuanping888.oss-cn-chengdu.aliyuncs.com/ddf7a29d-eac9-4b2b-a3d2-595fc64246af.jpg" 
            alt="Avatar"  
            class="avatar"  
            @click="showAvatar = true" 
          />
          <span class="username">Tom</span>

          <el-dropdown>
            <el-button type="primary" circle>
              <el-icon>
                <setting />
              </el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handlePasswordChange">修改密码</el-dropdown-item>
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main>
        <el-scrollbar>
          <el-table :data="tableData">
            <el-table-column prop="date" label="日期" width="140" />
            <el-table-column prop="name" label="姓名" width="120" />
            <el-table-column prop="address" label="地址" />
          </el-table>
        </el-scrollbar>
      </el-main>

      <el-dialog :visible.sync="showAvatar" width="400px">
        <img 
          src="https://daiyuanping888.oss-cn-chengdu.aliyuncs.com/ddf7a29d-eac9-4b2b-a3d2-595fc64246af.jpg" 
          alt="Avatar" 
          class="large-avatar" 
        />
        <span slot="footer" class="dialog-footer">
          <el-button @click="saveAvatar">保存头像</el-button>
          <el-button @click="showAvatar = false">关闭</el-button>
        </span>
      </el-dialog>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      showAvatar: false,
      tableData: [
        { date: '2016-05-02', name: 'Tom', address: 'No. 1, Block A' },
        { date: '2016-05-04', name: 'Jerry', address: 'No. 2, Block B' },
        { date: '2016-05-01', name: 'John', address: 'No. 3, Block C' },
      ],
    };
  },
  methods: {
    handlePasswordChange() {
      console.log('修改密码');
    },
    handleLogout() {
      console.log('退出登录');
    },
    saveAvatar() {
      const link = document.createElement('a');
      link.href = 'https://daiyuanping888.oss-cn-chengdu.aliyuncs.com/ddf7a29d-eac9-4b2b-a3d2-595fc64246af.jpg';
      link.download = 'avatar.jpg'; 
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },
  },
  mounted() {
    this.observer = new ResizeObserver(entries => {
      requestAnimationFrame(() => {
        for (let entry of entries) {
          console.log('Size changed:', entry.contentRect);
        }
      });
    });

    this.observer.observe(this.$el);
  },
  beforeDestroy() {
    if (this.observer) {
      this.observer.disconnect();
    }
  }
};
</script>

<style scoped>
.layout-container-demo {
  border: 1px solid #eaeaea;
  height: 100vh;
  display: flex;
}

.sidebar {
  background-color: #ffffff;
  border-right: 1px solid #eaeaea;
}

.custom-menu {
  background-color: #f5f7fa;
}

.el-menu-item,
.el-sub-menu__title {
  font-size: 14px;
  color: #333;
}

.el-menu-item:hover {
  background-color: #e6f7ff;
  color: #007bff;
}

.header {
  text-align: right;
  background-color: #007bff;
  color: #fff;
  padding: 10px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between; /* 使头像和用户名均匀分布 */
  width: 100%;
}

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 10px;
  cursor: pointer;
}

.large-avatar {
  width: 100%;
}

.dialog-footer {
  text-align: right;
}

@media (max-width: 768px) {
  .sidebar {
    display: none;
  }

  .layout-container-demo {
    flex-direction: column;
  }

  .header {
    text-align: left;
  }
}
</style>